﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NHibernate;
using TanashCMS.Business;
using TanashCMS.Business.Task;
using TanashCMS.Business.DTO;

public partial class manager_dialogs_navigation_Default : CMSManagementPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DataBind();
    }

    private Navigation CreateNavigation()
    {
        if (mWebsite == null)
        {
            ShowSystemFeedback("You need to select a website");
            return null;
        }

        ISession session = Configuration.GetCurrentSession();

        Navigation nav = new Navigation();
        if (radioNavigationType.SelectedItem == null)
        {

            return null;
        }

        if (radioNavigationType.SelectedValue == "WebPage")
        {
            WebPage webpage = session.Get<WebPage>(int.Parse(ddlTargetID.SelectedValue));
            if (ddlTargetID.SelectedIndex != 0)
            {
                nav.TargetID = int.Parse(ddlTargetID.SelectedValue);
                nav.Title = webpage.Title;
            }
            if (webpage.ItemKey != "")
                nav.NavigateURL = Request.ApplicationPath.TrimEnd('/') + "/" + webpage.ItemKey + ".html";
            else
                nav.NavigateURL = Request.ApplicationPath.TrimEnd('/') + "/?guid=" + ((int)CMSObjectType.WebPage).ToString().PadLeft(2, '0') + webpage.GUID;
        }
        else if (radioNavigationType.SelectedValue == "URL")
            nav.NavigateURL = txtURL.Text;
        else if (radioNavigationType.SelectedValue == "category")
        {
            Category cat = session.Get<Category>(int.Parse(ddlSelectCategory.SelectedValue));
            nav.TargetType = CMSObjectType.Category;
            nav.TargetID = int.Parse(ddlSelectCategory.SelectedValue);
            nav.Title = cat.Title;
            nav.NavigateURL = Request.ApplicationPath.TrimEnd('/') + "/?guid=" + ((int)CMSObjectType.Category).ToString().PadLeft(2, '0') + cat.GUID;
        }

        if (radioPosition.SelectedValue == "child")
        {
            nav.ParentID = int.Parse(ddlPosition.SelectedValue);
        }

        nav.GUID = Guid.NewGuid().ToString();
        nav.Website = mWebsite;
        if (txtText.Text != "")
            nav.Title = txtText.Text;
        nav.State = CMSObjectState.Published;

        return nav;
    }

    protected void btnAddToNav_Click(object sender, EventArgs e)
    {
        Navigation nav = CreateNavigation();
        if (nav == null)
            return;

        ISession session = Configuration.GetCurrentSession();

        session.Save(nav);

        mWebsite.NeedsPublish = true;
        session.SaveOrUpdate(mWebsite);
        //CMSMasterPage.setPublishLinkVisibility(true);

        session.Flush();

        /*if (tvNavigation.SelectedNode != null)
            selectedNodeValue = tvNavigation.SelectedNode.Value;

        tvNavigation.Nodes.Clear();*/
        ResetForm();
        DataBind();

        /*btnShowNewNavForm.Enabled = true;*/

        ShowSystemFeedback("The navigation item was added to the menu");
    }

    protected void odsWebPages_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (mWebsite != null)
            e.InputParameters["WebsiteID"] = mWebsite.ID;
    }

    protected void btnCreateNewPage_Click(object sender, EventArgs e)
    {
        ISession session = Configuration.GetCurrentSession();
        ITransaction tx = session.BeginTransaction();

        try
        {
            WebPage newPage = new WebPage();
            newPage.GUID = Guid.NewGuid().ToString();
            newPage.Website = mWebsite;
            newPage.Title = txtPageTitle.Text;
            newPage.State = CMSObjectState.Published;
            newPage.ObjectType = CMSObjectType.WebPage;
            newPage.PageType = WebPageType.Standard;
            newPage.MasterPage = "default";

            mWebsite.NeedsPublish = true;
            session.SaveOrUpdate(mWebsite);
            CMSMasterPage.setPublishLinkVisibility(true);

            session.Save(newPage);
            session.Flush();
            tx.Commit();
            ddlTargetID.Items.Clear();
            ddlTargetID.DataBind();
            ddlTargetID.SelectedValue = newPage.ID.ToString();

            session.Disconnect();
        }
        catch (Exception ex)
        {
            tx.Rollback();
            session.Disconnect();
            ShowSystemFeedback(ex.Message);
        }
    }

    protected void radioNavigationType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (radioNavigationType.SelectedValue == "WebPage")
        {
            panelWebPage.Visible = true;
            panelExternalURL.Visible = false;
            panelCategory.Visible = false;
        }
        else if (radioNavigationType.SelectedValue == "URL")
        {
            panelExternalURL.Visible = true;
            panelWebPage.Visible = false;
            panelCategory.Visible = false;
        }
        else if (radioNavigationType.SelectedValue == "category")
        {
            panelCategory.Visible = true;
            panelWebPage.Visible = false;
            panelExternalURL.Visible = false;
        }
        if (radioPosition.SelectedIndex > -1)
            btnAddToNav.Enabled = true;
    }

    protected void radioPosition_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (radioPosition.SelectedValue)
        {
            case "root":
                ddlPosition.Enabled = false; break;
            case "child":
                ddlPosition.Enabled = true; break;
        }
        if (radioPosition.SelectedIndex > -1)
            btnAddToNav.Enabled = true;

    }

    private void BindPositionChildItems(int ParentID, int Depth)
    {
        ISession session = Configuration.GetCurrentSession();
        List<Navigation> navItems = (List<Navigation>)session.CreateQuery("select n from Navigation n where n.Website.ID = " + mWebsite.ID.ToString() + " and n.ParentID = " + ParentID.ToString()).List<Navigation>();

        string indent = "";
        for (int i = 0; i < Depth; i++)
            indent += "----";

        foreach (Navigation nav in navItems)
        {
            ListItem listItem = new ListItem(indent + nav.Title, nav.ID.ToString());
            ddlPosition.Items.Add(listItem);
            BindPositionChildItems(nav.ID, Depth++);
        }
    }

    protected void ddlPosition_DataBinding(object sender, EventArgs e)
    {
        ISession session = Configuration.GetCurrentSession();
        List<Navigation> navItems = (List<Navigation>)session.CreateQuery("select n from Navigation n where n.Website.ID = " + mWebsite.ID.ToString() + " and n.ParentID = 0").List<Navigation>();
        foreach (Navigation nav in navItems)
        {
            ddlPosition.Items.Add(new ListItem(nav.Title, nav.ID.ToString()));
            BindPositionChildItems(nav.ID, 1);
        }
    }
    protected void btnCancelAddToNav_Click(object sender, EventArgs e)
    {
        panelNewNavigation.Visible = false;
        //btnShowNewNavForm.Enabled = true;
        ResetForm();
    }

    private void ResetForm()
    {
        txtText.Text = "";
        txtPageTitle.Text = "";
        if (ddlPosition.Items.Count > 0)
            ddlPosition.SelectedIndex = 0;
        radioPosition.SelectedIndex = 0;
        ddlPosition.Enabled = false;
        if (ddlTargetID.Items.Count > 0)
            ddlTargetID.SelectedIndex = 0;
        btnAddToNav.Enabled = false;
        panelExternalURL.Visible = false;
        panelWebPage.Visible = false;
        txtURL.Text = "http://";
        radioNavigationType.SelectedIndex = -1;
        radioPosition.SelectedIndex = -1;
        panelCategory.Visible = false;
    }
}
